<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="../lib/ext/resources/css/ext-all-neptune.css"/>
    <link rel="stylesheet" type="text/css" href="../lib/ckeditor/sample.css" />
    <script type="text/javascript" src="../lib/ext/ext-all.js"></script>
    <script type="text/javascript" src="../lib/ckeditor/ckeditor.js"></script>
    <script type="text/javascript" src="../lib/ckeditor/sample.js"></script>
    <script type="text/javascript" src="../lib/ext_ux_ckeditor.js"></script>
    <script type="text/javascript" src="../lib/gf/common_grid.js"></script>
</head>
<script type="text/javascript">
    Ext.Loader.setConfig({enabled: true});
    var dataForm;
    var data = {};
    var image_qrcode = Ext.create('Ext.Img', {
        src: '',
        height : 100
    });
    Ext.onReady(function () {
        //保存数据
        function doAdd(){
            var params = dataForm.form.getFieldValues();
            var _url = '/qrcode/add';
            if(params && params._id ){
                _url = '/qrcode/edit';
            }
            if(!params.url || !params.image_file_id){
                alert('链接或文件id不得为空');
                return;
            }
            Ext.Ajax.request({
                url: _url,
                params : params,
                method : 'POST',
                success: function(response, opts) {
                    alert('业务处理成功');
                    parent.refresh();
                    parent.closeWin();
                },
                failure: function(response, opts) {
                    alert('业务处理失败');
                }
            });

        }
        //关闭窗口
        function doClose(){
            parent.closeWin();
        }
        var tbar = [
            {pressed: true,text: '保  存',handler: doAdd,icon : '../img/book.png'},
            {pressed: true,text: "关  闭",handler: doClose,icon : '../img/close.png'}
        ];
        dataForm = new Ext.FormPanel({
            layout : 'fit',
            items: [
                {
                    xtype: 'form',

                    defaultType: 'textfield',
                    items: [
                        {
                            name : '_id',
                            hidden : true
                        },
                        {
                            fieldLabel: '链接',
                            name : 'url',
                            width : 500
                        },
                        {fieldLabel: '二维码图片id',name: 'image_file_id',readOnly : true,width : 500},
                        {xtype: "filefield",width:500,fieldLabel: '二维码图片',name: 'attach_file'},
                        image_qrcode,
                        {xtype : "label",text: ''},
                        {
                            xtype: "button",
                            text: "上传二维码",
                            width: 100,
                            handler: function () {
                                uploadFile(dataForm,"",image_qrcode);
                            }
                        },

                        {xtype : "label",text: '二维码描述'},
                        {
                            xtype : 'textareafield',
                            name : 'description',
                            fieldLabel : '',
                            width : '100%'
                        }
                    ],
                    tbar : tbar
                }
            ]
        });
        var viewport = new Ext.Viewport({
            layout: 'fit',
            renderTo: "mypanel",
            items: [dataForm]
        });



        //获取url中的参数，如果传入了参数_id那么进行修改；否则，添加。
        var _urlparams = Ext.urlDecode(document.URL.split("?")[document.URL.split("?").length - 1]);
        if(_urlparams && _urlparams._id){
            Ext.Ajax.request({
                url: '/qrcode/detail',
                params : {'_id' : _urlparams._id},
                method : 'get',
                success: function(response, opts) {
                    data =  Ext.decode(response.responseText);
                    init_form();
                },
                failure: function(response, opts) {
                    alert('加载失败......');
                    init_form();
                }
            });
        }

        function init_form(){
            if(!data){
                return ;
            }
            for(var i in data){
                if(dataForm.form.findField(i)){
                    dataForm.form.findField(i).setValue(data[i]);
                }
            }
            if(data.image_file_id){
                image_qrcode.setSrc('/_image/' + data.image_file_id);
            }

        }
    });
</script>
<body>
<div id="mypanel" style="height:1024px;width:100%"></div>
</body>
</html>